package com.xhwl.logistics.strategy.impl;


import com.xhwl.common.dto.cent.vehicle.feign.CreateBiVehicleAccessRecordHourFeignDTO;
import com.xhwl.common.dto.cent.vehicle.feign.CreateBiVehicleCarSpotHourFeignDTO;
import com.xhwl.common.vo.cent.vehicle.bi.VehicleCarSpotHourVO;
import com.xhwl.logistics.bo.VehicleCarportBO;
import com.xhwl.logistics.bo.VehicleParkingLotSecondBO;
import com.xhwl.logistics.bo.cent.VehicleIntegratedConfigBO;
import com.xhwl.logistics.bo.input.QueryVehicleCarportBO;
import com.xhwl.logistics.bo.manipulate.VehicleSyncBO;
import com.xhwl.logistics.constant.VehicleConstant;
import com.xhwl.logistics.converter.mapstruct.CentBiConverter;
import com.xhwl.logistics.converter.mapstruct.VehicleAccessRecordConverter;
import com.xhwl.logistics.service.IVehicleAccessRecordService;
import com.xhwl.logistics.service.IVehicleParkingLotService;
import com.xhwl.logistics.service.cent.IBIVehicleService;
import com.xhwl.logistics.strategy.IVehicleParkingLotHandler;
import com.xhwl.logistics.util.AboutDateUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;

@Slf4j
//@Service
public class VehicleParkingLotHuaWeiIOTHandlerImpl implements IVehicleParkingLotHandler {

    @Autowired
    private IVehicleParkingLotService vehicleParkingLotService;

    @Autowired
    private IVehicleAccessRecordService vehicleAccessRecordService;

    @Autowired
    private VehicleAccessRecordConverter vehicleAccessRecordConverter;

    @Autowired
    private CentBiConverter centBiConverter;

    @Autowired
    private IBIVehicleService biVehicleService;

    @Override
    public void sync(VehicleIntegratedConfigBO vehicleIntegratedConfigBO, VehicleSyncBO vehicleSyncBO) {

    }

    @Override
    public VehicleCarportBO queryCarport(VehicleIntegratedConfigBO vehicleIntegratedConfigBO, QueryVehicleCarportBO queryVehicleCarportBO) {
        return null;
    }

    @Override
    public void statisticsToBi(VehicleIntegratedConfigBO vehicleIntegratedConfigBO) {
        //获取当前时间
        Date date = new Date();
        //获取华为IOT对应的项目id
        Integer projectId = vehicleIntegratedConfigBO.getVehicleSystemInfoBO().getProjectId();
        //获取华为IOT的项目下所有的车场信息
        List<VehicleParkingLotSecondBO> vehicleParkingLotSecondBOS = vehicleParkingLotService.queryParkingLots(projectId, null);
        if(vehicleParkingLotSecondBOS.isEmpty()){
            log.info("该项目下无停车场");
            return;
        }
        //创建华为IOT项目下的所有统计记录的集合，避免在for循环中操作数据库
        List<CreateBiVehicleAccessRecordHourFeignDTO> accessRecordHourFeignDTOList = new LinkedList<>();
        //对华为IOT的项目的出行记录统计近一个小时的数据，例如：现在时间为14::20，那么就统计13::00到14::00的数据
        vehicleParkingLotSecondBOS.forEach(vehicleParkingLotSecondBO -> {
            //获取第三方的停车场id
            String parkId = vehicleParkingLotSecondBO.getParkId();
            //获取车场名字
            String parkingLotName = vehicleParkingLotSecondBO.getParkingLotName();
            //获取当前时间的上个小时的整点时间，例现在时间为14::20，获得为13::00
            Date date1 = AboutDateUtil.dateMinusHour(AboutDateUtil.theSameDayHourOfDate(date, VehicleConstant.vehicleTimeZone), 1);
            log.info("统计时间："+date1);
            //获得符合要求的进入车辆的数目
            Integer inCount = vehicleAccessRecordService.accessRecordForCountWU(vehicleAccessRecordConverter.toQueryVehicleAccessRecordForCountBOForEnter(projectId, parkId, date1));
            //获得符合要求的出去车辆的数目
            Integer outCount = vehicleAccessRecordService.accessRecordForCountWU(vehicleAccessRecordConverter.toQueryVehicleAccessRecordForCountBOForGoOut(projectId, parkId, date1));
            //算出车辆进出场的总数
            Integer totalCount = inCount + outCount;
            log.info("进次数："+inCount+"出记录："+outCount+"总计录："+totalCount);
            //将这些数据组装成一条记录，并放到List集合中
            accessRecordHourFeignDTOList.add(centBiConverter.toCreateBiVehicleAccessRecordHourFeignDTO(projectId,"车场",totalCount,inCount,outCount,date,date1));
        });
        //去掉其中重复的记录
        List<CreateBiVehicleAccessRecordHourFeignDTO> accessRecordHourFeignDTOS =new LinkedList<>();
        for (CreateBiVehicleAccessRecordHourFeignDTO createBiVehicleAccessRecordHourFeignDTO : accessRecordHourFeignDTOList) {
            CreateBiVehicleAccessRecordHourFeignDTO createBiVehicleAccessRecordHourFeignDTO1 = accessRecordHourFeignDTOS.stream().filter(s -> {
                if (StringUtils.equals(s.getProjectId().toString(), createBiVehicleAccessRecordHourFeignDTO.getProjectId().toString())) {
                    return true;
                } else {
                    return false;
                }
            }).findAny().orElse(null);
            if(createBiVehicleAccessRecordHourFeignDTO1==null){
                accessRecordHourFeignDTOS.add(createBiVehicleAccessRecordHourFeignDTO);
            }
        }
        //查询是否已经统计过了
        Date ingestionTime3 = AboutDateUtil.dateMinusHour(AboutDateUtil.theSameDayHourOfDate(date, VehicleConstant.vehicleTimeZone), 1);
        CreateBiVehicleCarSpotHourFeignDTO createBiVehicleCarSpotHourFeignDTO1 = new CreateBiVehicleCarSpotHourFeignDTO();
        createBiVehicleCarSpotHourFeignDTO1.setIngestionTime(ingestionTime3);
        List<VehicleCarSpotHourVO> vehicleCarSpotHourVOS1 = biVehicleService.checkCarSpotHour(createBiVehicleCarSpotHourFeignDTO1);
        //获取上一次统计时间
        Date ingestionTime2 = AboutDateUtil.dateMinusHour(AboutDateUtil.theSameDayHourOfDate(date, VehicleConstant.vehicleTimeZone), 2);
        CreateBiVehicleCarSpotHourFeignDTO createBiVehicleCarSpotHourFeignDTO = new CreateBiVehicleCarSpotHourFeignDTO();
        createBiVehicleCarSpotHourFeignDTO.setIngestionTime(ingestionTime2);
        //查询上一次统计时间所对应的所有车位统计
        List<VehicleCarSpotHourVO> vehicleCarSpotHourVOS = biVehicleService.checkCarSpotHour(createBiVehicleCarSpotHourFeignDTO);
        //利用这个统计数据来对车位进行统计
        List<CreateBiVehicleCarSpotHourFeignDTO> createBiVehicleCarSpotHourFeignDTOS = new LinkedList<>();
        for (CreateBiVehicleAccessRecordHourFeignDTO accessRecordHourFeignDTO : accessRecordHourFeignDTOS) {
            VehicleCarSpotHourVO vehicleCarSpotHourVO = vehicleCarSpotHourVOS.stream().filter(s -> {
                if (StringUtils.equals(s.getProjectId().toString(), accessRecordHourFeignDTO.getProjectId().toString())) {
                    return true;
                } else {
                    return false;
                }
            }).findAny().orElse(null);
            CreateBiVehicleCarSpotHourFeignDTO vehicleCarSpotHourFeignDTO = new CreateBiVehicleCarSpotHourFeignDTO();
            //有上一次统计记录
            if(vehicleCarSpotHourVO!=null){
                vehicleCarSpotHourFeignDTO.setCarExist(vehicleCarSpotHourVO.getCarExist() + accessRecordHourFeignDTO.getIn() - accessRecordHourFeignDTO.getOut());
                vehicleCarSpotHourFeignDTO.setCarSpotFree(vehicleCarSpotHourVO.getCarSpotFree()-accessRecordHourFeignDTO.getIn()+accessRecordHourFeignDTO.getOut());
                vehicleCarSpotHourFeignDTO.setCarSpotTotal(23);
                vehicleCarSpotHourFeignDTO.setIngestionTime(accessRecordHourFeignDTO.getIngestionTime());
                vehicleCarSpotHourFeignDTO.setParkingName(accessRecordHourFeignDTO.getParkingName());
                vehicleCarSpotHourFeignDTO.setProjectId(accessRecordHourFeignDTO.getProjectId());
            }
            //无上一次统计记录，即初始化的时候
            else{
                vehicleCarSpotHourFeignDTO.setCarExist(accessRecordHourFeignDTO.getIn() - accessRecordHourFeignDTO.getOut());
                vehicleCarSpotHourFeignDTO.setCarSpotFree(23-accessRecordHourFeignDTO.getIn()+accessRecordHourFeignDTO.getOut());
                vehicleCarSpotHourFeignDTO.setCarSpotTotal(23);
                vehicleCarSpotHourFeignDTO.setIngestionTime(accessRecordHourFeignDTO.getIngestionTime());
                vehicleCarSpotHourFeignDTO.setParkingName(accessRecordHourFeignDTO.getParkingName());
                vehicleCarSpotHourFeignDTO.setProjectId(accessRecordHourFeignDTO.getProjectId());
            }
            createBiVehicleCarSpotHourFeignDTOS.add(vehicleCarSpotHourFeignDTO);
        }
        if(vehicleCarSpotHourVOS1.isEmpty()){
            //调用BI中台的服务保存车位统计数据
            biVehicleService.addDwsCarSpotForHour(createBiVehicleCarSpotHourFeignDTOS);
        }else{
            //虽然这里是for循环，但是循环的是使用了华为IOT的项目数，故次数很少。
            List<CreateBiVehicleCarSpotHourFeignDTO> createBiVehicleCarSpotHourFeignDTOS2 = new LinkedList<>();
            for (CreateBiVehicleCarSpotHourFeignDTO biVehicleCarSpotHourFeignDTO : createBiVehicleCarSpotHourFeignDTOS) {
                //查看是否统计过，统计过就更新统计数据，反之就新增。
                VehicleCarSpotHourVO vehicleCarSpotHourVO = vehicleCarSpotHourVOS1.stream().filter(s -> {
                    if (StringUtils.equals(s.getProjectId().toString(),biVehicleCarSpotHourFeignDTO.getProjectId().toString())) {
                        return true;
                    } else {
                        return false;
                    }
                }).findAny().orElse(null);
                if(vehicleCarSpotHourVO!=null){
                    biVehicleService.alterDwsCarSpotForHour(biVehicleCarSpotHourFeignDTO);
                }else{
                    createBiVehicleCarSpotHourFeignDTOS2.add(biVehicleCarSpotHourFeignDTO);
                }
            }
            biVehicleService.addDwsCarSpotForHour(createBiVehicleCarSpotHourFeignDTOS2);
        }
    }
}
